//
//  ZDShoppCarCell.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/16.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDShoppCarCell.h"
#import "UIImageView+WebCache.h"

@interface ZDShoppCarCell()

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *consumeTypeIcon;
@property (weak, nonatomic) IBOutlet UIButton *selectButton;
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@property (weak, nonatomic) IBOutlet UILabel *goodsNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UITextField *inputField;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UIButton *minusButton;
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
/**
 <#Description#>
 */
@property(nonatomic,strong)shopCarOpertionBlock block;

@end

@implementation ZDShoppCarCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setModel:(ZDShoppCarGoodsModel *)model
{
    _model = model;
    self.nameLabel.text = model.store_name;
    [self.icon sd_setImageWithURL:[NSURL URLWithString:model.goods_image_url]];
    self.goodsNameLabel.text = model.goods_name;
    self.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.goods_price];
    self.inputField.text = model.goods_num;
    UIImage *image = [model.door_service isEqualToString:@"1"] ? [UIImage imageNamed:@"smfw"]:[UIImage imageNamed:@"ddxf"];
    self.consumeTypeIcon.image = image ;
    
    //编辑状态
    self.deleteButton.hidden = !model.isEditing;
    self.selectButton.hidden = model.isEditing;
    self.minusButton.hidden = model.isEditing;
    self.addButton.hidden = model.isEditing;
    
    //选择状态
    UIImage *selectImage = model.isSelecting?[UIImage imageNamed:@"selected"] : [UIImage imageNamed:@"unSelected"];
    [self.selectButton setImage:selectImage forState:0];
    
}

- (void)shopCarOpertion:(shopCarOpertionBlock)block
{
    _block = block;
}
#pragma mark- 点击事件
/**
 删除点击

 @param sender <#sender description#>
 */
- (IBAction)deleteClick:(UIButton *)sender
{
    _block(3,_model);
}

/**
 选择点击

 @param sender <#sender description#>
 */
- (IBAction)selectClick:(UIButton *)sender
{

    _model.isSelecting = !_model.isSelecting;
    //设置界面
    UIImage *image = _model.isSelecting ? [UIImage imageNamed:@"selected"] : [UIImage imageNamed:@"unSelected"];
    [self.selectButton setImage:image forState:0];
    _block(1,_model);
}

/**
 点击减号

 @param sender <#sender description#>
 */
- (IBAction)minusClick:(UIButton *)sender
{
    int num = [self.inputField.text intValue];
    if (num == 1) {
        [SVProgressHUD showInfoWithStatus:@"最少数量为 1"];
        return;
    }
    _model.goods_num = [NSString stringWithFormat:@"%d",(--num)];
    _block(2,_model);
}

/**
 点击加号

 @param sender <#sender description#>
 */
- (IBAction)addClick:(UIButton *)sender
{
    int num = [self.inputField.text intValue];
    _model.goods_num = [NSString stringWithFormat:@"%d",(++num)];
    _block(2,_model);
}

@end
